<?php
namespace app\api\controller;

use app\BaseController;
use app\model\Article as A;
use app\model\Journal as J;
use app\model\ArticleStar as S;
use think\facade\Db;

use think\cache\driver\Redis;
class Article extends ApiBase
{
    public function homeList()
    {
        $article = (new A())->where('status',1)->where('is_home',1)->order('create_time desc')->paginate(input('limit/d'));
        $article = $article->toArray();
        if ($article['total'] == 0) {
            return $this->returnApi([],1,'暂无文章');
        }
        $data = [];
        foreach ($article['data'] as &$v) {
            $data[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'img' => $v['img'],
                'composition' => $v['composition'],
                'create_time' => date('Y-m-d',$v['create_time']),
                "author" => $v['author'],
                "author_type" => $v['author_type'],
            ];
        }
        $res = [
            'data' => $data,
            'page' => $article['current_page'],
            'total' => $article['total'],
        ];

        return $this->returnApi($res,0,'成功');
    }


    //列表搜索
    public function search()
    {
        $keyword = input('keyword');
        $where = [];
        $where[] = ["title","like","%".$keyword."%"];
        $article = (new A())->where($where)->select()->toArray();
        if (empty($article)) {
            return $this->returnApi([],1,'没有相关文章');
        }
        $data = [];
        foreach ($article as &$v) {
            $data[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'img' => $v['img'],
                'composition' => $v['composition'],
                'create_time' => date('Y-m-d',$v['create_time']),
                "author" => $v['author'],
                "author_type" => $v['author_type'],
            ];
        }
        return $this->returnApi($data,0,'成功');
    }

    public function list()
    {
        $author_type = input('author_type/d');
        $query = (new A())->where("status",1);
        if($author_type){
            $query = $query->where('author_type',$author_type);
        }
        $article = $query->order('create_time desc')->paginate(input('limit/d'));
        $article = $article->toArray();
        if ($article['total'] == 0) {
            return $this->returnApi([],0,'暂无文章');
        }
        $data = [];
        foreach ($article['data'] as &$v) {
            $data[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'img' => $v['img'],
                'composition' => $v['composition'],
                'create_time' => date('Y-m-d',$v['create_time']),
                "author" => $v['author'],
                "author_type" => $v['author_type'],
            ];
        }
        $res = [
            'data' => $data,
            'page' => $article['current_page'],
            'total' => $article['total'],
        ];
        return $this->returnApi($res,0,'成功');
    }

    //详情
    public function detail()
    {
        $article_id = input('id/d');
        if (!$article_id) {
            return $this->returnApi([], 1, '参数错误');
        }
        $article = (new A())->where('id',$article_id)->find();
        if(!$article){
            return $this->returnApi(null,1,'文章不存在');
        }
        $data = [
            'id' => $article['id'],
            'title' => $article['title'],
            'img' => $article['img'],
            'create_time' => date('Y-m-d',$article['create_time']),
            "author" => $article['author'],
            "author_type" => $article['author_type'],
            "desc" => $article['desc'],
            "brief" => $article['brief'],
            "composition" => $article['composition'],
        ];
        return $this->returnApi($data,0,'成功');
    }
}




